package com.coderlin.service.user;

import com.coderlin.dao.BaseDao;
import com.coderlin.dao.user.UserDao;
import com.coderlin.dao.user.UserDaoImpl;
import com.coderlin.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * <Description> <br>
 *
 * @author Coder_Lin<br>
 * @version 1.0<br>
 * @taskId: <br>
 * @createDate 2021/03/12 21:03 <br>
 * @see com.coderlin.service <br>
 */
public class UserServiceImpl implements UserService {

    //业务层都会调用DAO层，所以需要引入Dao层

    private UserDao userDao;

    public UserServiceImpl(){
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(String userCode, String password) {

        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection, userCode);

            //添加对密码的匹配
            if(!user.getUserPassword().equals(password)){
                user = null;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //资源关闭
            BaseDao.closeResource(connection,null,null);
        }

        return user;
    }


    /**
     * @author:Coderlin
     * 单元测试，测试当前编写的User类，数据库Dao的Model层和Service层是否可以正常使用
     * */
//    @Test
//    public void test(){
//        UserServiceImpl userService = new UserServiceImpl();
//        User user = userService.login("admin", "123231234567");
//        System.out.println(user.getUserPassword());
//    }

    @Override
    public boolean updatePwd(int id, String userPassword) {
        boolean flag = false;
        //获取数据库连接
        Connection connection = BaseDao.getConnection();
        //创建操作数据库对象,已经拥有该成员作为私有对象了
        try {
            if(userDao.updatePwd(connection,id,userPassword)>0){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //关闭数据库资源
            BaseDao.closeResource(connection,null,null);
        }

        return flag;
    }


    /**测试功能，查看对数据库操作的Model层是否工作正常
     * */
//    @Test
//    public void test(){
//        UserService userService = new UserServiceImpl();
//        System.out.println(userService.updatePwd(2,"123456789"));
//    }

    @Override
    public int getUserCount(String username, int userRole) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection, username, userRole);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return count;
    }

    /**单元测试查询getUserCount是否工作正常
     * */
//    @Test
//    public void test(){
//        UserService userService = new UserServiceImpl();
//        System.out.println(userService.getUserCount(null, 0));
//    }


    @Override
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = new ArrayList<>();


        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection, queryUserName, queryUserRole,currentPageNo,pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return  userList;
    }

    /**单元测试，测试getUserList是否成功工作
     * */
//    @Test
//    public void test(){
//        UserService userService = new UserServiceImpl();
//        List<User> userList = userService.getUserList(null, 0, 1, 5);
//        for (User user: userList){
//            System.out.println(user.toString());
//            System.out.println("===============================");
//        }
//    }
}
